<template>
    <div class="home">
        <!--    <img alt="Vue logo" style="width: 100%"  fit="contain" :src="require('@/assets/images/bg.jpg')">-->
        <van-swipe :autoplay="3000" style="width: 100%;height: 200px" @change="onChange" lazy-render>

            <van-swipe-item v-for="(image, index) in images" :key="index">
                <img :src="image" fit="cover" @click="tourl" style="width: 100%;height: 100%" />
            </van-swipe-item>
        </van-swipe>
        <NoticebarCom ref="noticecom"></NoticebarCom>
        <van-swipe-cell>
            <van-card class="my-van-card" :tag="tag" title="项目信息">
                <template v-slot:desc>
                    <div class="custom-desc">项目名称：{{ prjname }}</div>
                </template>
                <template #footer>
                    <van-button icon="plus" size="small" type="primary" to="/addprj">新增</van-button>
                    <van-button v-if="this.maxnum > 1" icon="exchange" size="small" type="primary"
                        @click="changeprj()">切换</van-button>
                </template>
                <template #tags>
                    <van-tag class="mytag" plain type="primary">项目编号：{{ code }}</van-tag>
                </template>
            </van-card>
            <template #right>
                <van-button square text="删除" type="danger" class="delete-button" @click="delprj()" />
            </template>
        </van-swipe-cell>
        <wqzsp v-if="this.worktype!== undefined && this.worktype.includes('A') == true"></wqzsp>
        <glrykqsq v-if="this.worktype!== undefined && this.worktype.includes('G') == true"></glrykqsq>
        <Home ref="child" msg="Welcome to Your Vue.js App" />
    </div>
</template>
<style scoped>
.my-van-card {
    --van-card-text-color: red;
    text-align: left;
}

.mytag {
    padding: 2%;
}

.delete-button {
    height: 100%;
}

.custom-desc {
    color: red;
    font-size: 14px;
    padding: 2%;
    /* 在这里添加其他样式属性 */
}
</style>

<script>
// @ is an alias to /src
import Home from '@/components/Home.vue'
import Wqzsp from '@/components/Wqzsp.vue'
import Glrykqsq from '@/components/Glrykqsq.vue'
import NoticebarCom from "@/components/NoticebarCom";
import axios from "axios";

// 1. 引入你需要的组件
import { Button, Grid, GridItem, List, Swipe, SwipeItem, Cell, CellGroup, Card, Tag, SwipeCell, showConfirmDialog } from 'vant';
// 2. 引入组件样式
import 'vant/lib/index.css';



export default {
    name: 'HomeView',
    data() {
        return {
            prjname: '',
            code: '',
            worktype:'',
            tag: '',
            prjinfo: [],
            num: 0,
            maxnum: 0,
            currentIndex: 0,
            imageurl: [
                'https://mp.weixin.qq.com/s/NbzAK1XYcSHBAD_WHfMg5g',
                'https://mp.weixin.qq.com/s/PXzWV8hWv_m4pBcMlu4GPQ',
                'https://mp.weixin.qq.com/s/G6gv-_8kJtqdGJjrfl4YOg',
                'https://mp.weixin.qq.com/s/Xzuvwko4EpJQB1k0cs3Gbw'
            ]
        }
    },

    components: {
        Home,
        NoticebarCom,
        Wqzsp,
        Glrykqsq,
        [Button.name]: Button,
        [Grid.name]: Grid,
        [GridItem.name]: GridItem,
        [List.name]: List,
        [Swipe.name]: Swipe,
        [SwipeItem.name]: SwipeItem,
        [Cell.name]: Cell,
        [CellGroup.name]: CellGroup,
        [Card.name]: Card,
        [Tag.name]: Tag,
        [SwipeCell.name]: SwipeCell,
        [showConfirmDialog.name]: showConfirmDialog
    },
    setup() {
        const images = [
            require('@/assets/images/1.jpeg'),
            require('@/assets/images/2.jpeg'),
            require('@/assets/images/3.jpeg'),
            require('@/assets/images/4.jpeg'),
            // 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
        ];
        return {
            images,
            onChange(index) {
                console.log(index)
                sessionStorage.setItem('imageindex', index)
            }
        };
    },
    mounted() {
        sessionStorage.setItem('openid', this.$route.query['openid'])
        axios.post('http://bs.bzjsgl.cn/admin/getdata/getprjxxinfo', {
            openid: this.$route.query['openid']
        })
            .then(response => {
                if (!sessionStorage.getItem('num') || sessionStorage.getItem('num') == 'NaN') {
                    sessionStorage.setItem('num', 0)
                }
                console.log(sessionStorage);

                this.num = sessionStorage.getItem('num')
                this.prjinfo = response.data['data'],
                this.maxnum = response.data['data'].length,

                this.prjname = this.prjinfo[this.num]['name'],
                this.code = this.prjinfo[this.num]['code'],
                this.worktype = this.prjinfo[this.num]['worktype'],
                sessionStorage.setItem('code', this.code)
                if (this.prjinfo[this.num]['code'] == "0000000000000000000") {
                    this.tag = ''
                    sessionStorage.setItem('prjid', this.prjinfo[this.num]['id'])
                } else {
                    console.log('sessionStorage.setItem(prjid)')
                    this.tag = '已选'
                    sessionStorage.setItem('prjid', this.prjinfo[this.num]['id'])
                    this.$refs.child.getuserinfo()
                }
            })
            .catch(function (error) { // 请求失败处理
                console.log(error)
            })
    },
    methods: {
        changeprj() {
            if (this.num < (this.maxnum - 1)) {
                this.num = parseInt(sessionStorage.getItem('num')) + 1
                sessionStorage.setItem('num', this.num)
                console.log(this.num)
                this.prjname = this.prjinfo[this.num]['name'],
                this.code = this.prjinfo[this.num]['code'],
                this.worktype = this.prjinfo[this.num]['worktype'],
                sessionStorage.setItem('code', this.code)
                if (this.prjinfo[this.num]['code'] == "0000000000000000000") {
                    this.tag = ''
                    sessionStorage.setItem('prjid', this.$route.query['prjid'])
                } else {
                    this.tag = '已选'
                    sessionStorage.setItem('prjid', this.prjinfo[this.num]['id'])
                    this.$refs.child.getuserinfo()
                }
                console.log('+', this.num)
            } else {
                sessionStorage.setItem('num', 0)
                this.num = sessionStorage.getItem('num')
                this.prjname = this.prjinfo[this.num]['name'],
                this.code = this.prjinfo[this.num]['code'],
                this.worktype = this.prjinfo[this.num]['worktype'],
                sessionStorage.setItem('code', this.code)
                if (this.prjinfo[this.num]['code'] == "0000000000000000000") {
                    this.tag = ''
                    sessionStorage.setItem('prjid', this.$route.query['prjid'])
                } else {
                    this.tag = '已选'
                    sessionStorage.setItem('prjid', this.prjinfo[this.num]['id'])
                    this.$refs.child.getuserinfo()
                }
                console.log('-', this.num)
            }
        },
        delprj() {
            showConfirmDialog({
                message: '确定要删除此项目吗',
                theme: 'round-button',
            })
                .then(() => {
                    console.log(sessionStorage)
                    axios.post('http://bs.bzjsgl.cn/admin/getdata/delwx_outh_user', {
                        openid: sessionStorage.getItem('openid'),
                        prjid: sessionStorage.getItem('prjid')
                    })
                        .then(response => {
                            console.log(response.data['data'])
                            if (response.data['data'] === 1) {
                                axios.post('http://bs.bzjsgl.cn/admin/getdata/getprjxxinfo', {
                                    openid: this.$route.query['openid']
                                })
                                    .then(response => {
                                        if (!sessionStorage.getItem('num') || sessionStorage.getItem('num') == 'NaN') {
                                            sessionStorage.setItem('num', 0)
                                        }
                                        console.log(sessionStorage);
                                        this.num = sessionStorage.getItem('num')
                                        this.prjinfo = response.data['data'],
                                        this.maxnum = response.data['data'].length,
                                        this.worktype = this.prjinfo[this.num]['worktype'],
                                        this.prjname = this.prjinfo[this.num]['name'],
                                        this.code = this.prjinfo[this.num]['code'],
                                        sessionStorage.setItem('code', this.code)
                                        if (this.prjinfo[this.num]['code'] == "0000000000000000000") {
                                            this.tag = ''
                                            sessionStorage.setItem('prjid', this.prjinfo[this.num]['id'])
                                        } else {
                                            this.tag = '已选'
                                            sessionStorage.setItem('prjid', this.prjinfo[this.num]['id'])
                                        }
                                        this.$refs.child.getuserinfo()
                                    })
                                    .catch(function (error) { // 请求失败处理
                                        console.log(error)
                                    })

                            }
                        })
                        .catch(function (error) { // 请求失败处理
                            console.log(error)
                        })
                })
                .catch(() => {
                    console.log('cancel')
                    // on cancel
                });
        },
        tourl() {
            window.open(this.imageurl[sessionStorage.getItem('imageindex')]);
        },

    }
}
</script>
